import React, {PureComponent} from 'react';
import {
    PAGE,
    TABLE,
    DATETIME_FORMAT,
    PAGINATION_OPTIONS,
    COMMON_TABLE_EMPTY_TEXT,
    COMMON_TABLE_QUERY_FAIL_TEXT,
} from '../constants';
import {Table, Card} from 'antd';
import PropTypes from 'prop-types';
import {conventSorter} from './utils';
import {transportType, currency} from '../Enum';
import {formatDateTime, formatAmount, formatPercent, conventEnumValueToString} from '../utils';
export class TablePanel extends PureComponent {
    state = {
        loading: false,
    };
    onChangeSort = (pagination, filters, sorter) => {
        if(pagination.current - 1 === this.props.pageIndex && pagination.pageSize === this.props.pageSize)
            this.props.onChangeSort({...conventSorter(sorter),
                pageIndex: PAGE.index});
    };
    onSelectedChange = (rkeys, keys) => {
        this.setState({
            selectedRowKeys: rkeys,
            selectedRows: keys
        });
    };
    render() {
        const {formatMessage} = this.props.intl;
        const {pageIndex, pageSize, total} = this.props;
        const columns = [{
            title: formatMessage({
                id: 'tablePanel.column.dealerCode',
                defaultMessage: 'ERP号'
            }),
            dataIndex: 'dealerCode',
            sorter: true
        }, {
            title: formatMessage({
                id: 'tablePanel.column.marketDepartmentName',
                defaultMessage: '大区名称'
            }),
            dataIndex: 'marketDepartmentName',
            sorter: true
        }, {
            title: formatMessage({
                id: 'tablePanel.column.country',
                defaultMessage: '国家'
            }),
            dataIndex: 'country',
            sorter: true
        }, {
            title: formatMessage({
                id: 'tablePanel.column.code',
                defaultMessage: '订单号'
            }),
            dataIndex: 'code',
            sorter: true
        }, {
            title: formatMessage({
                id: 'tablePanel.column.submitDate',
                defaultMessage: '订单提报日期'
            }),
            dataIndex: 'submitDate',
            render: text => formatDateTime(text, DATETIME_FORMAT)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.fobTotalAmount',
                defaultMessage: 'FOB金额'
            }),
            dataIndex: 'fobTotalAmount',
            render: text => formatAmount(text)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.itemCount',
                defaultMessage: '订单品种'
            }),
            dataIndex: 'itemCount'
        }, {
            title: formatMessage({
                id: 'tablePanel.column.quantity',
                defaultMessage: '零件数量'
            }),
            dataIndex: 'quantity'
        }, {
            title: formatMessage({
                id: 'tablePanel.column.confirmTime',
                defaultMessage: 'PI确认日期'
            }),
            dataIndex: 'confirmTime',
            render: text => formatDateTime(text, DATETIME_FORMAT)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.creditAuditedTime',
                defaultMessage: '付款日期'
            }),
            dataIndex: 'creditAuditedTime',
            render: text => formatDateTime(text, DATETIME_FORMAT)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.transportType',
                defaultMessage: '运输方式'
            }),
            dataIndex: 'transportType',
            render: text => conventEnumValueToString(transportType, text)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.shipmentNumber',
                defaultMessage: '发运次数'
            }),
            dataIndex: 'shipmentNumber'
        }, {
            title: formatMessage({
                id: 'tablePanel.column.partInvoiceCode',
                defaultMessage: '发票号'
            }),
            dataIndex: 'partInvoiceCode'
        }, {
            title: formatMessage({
                id: 'tablePanel.column.invoiceCreateDate',
                defaultMessage: '发票生成日期'
            }),
            dataIndex: 'invoiceCreateDate',
            render: text => formatDateTime(text, DATETIME_FORMAT)
        }, {
            title: this.props.intl.formatMessage({
                id: 'tablePanel.column.currency',
                defaultMessage: '币种'
            }),
            dataIndex: 'currency',
            render: text => conventEnumValueToString(currency, text)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.itemCountInvoice',
                defaultMessage: '发运品种'
            }),
            dataIndex: 'itemCountInvoice'
        }, {
            title: formatMessage({
                id: 'tablePanel.column.totalAmountInvoice',
                defaultMessage: '发运金额'
            }),
            dataIndex: 'totalAmountInvoice',
            render: text => formatAmount(text)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.totalQuantityInvoice',
                defaultMessage: '发运数量'
            }),
            dataIndex: 'totalQuantityInvoice'
        }, {
            title: formatMessage({
                id: 'tablePanel.column.addupItemCountInvoice',
                defaultMessage: '累计发运品种'
            }),
            dataIndex: 'addupItemCountInvoice'
        }, {
            title: formatMessage({
                id: 'tablePanel.column.addupTotalAmountInvoice',
                defaultMessage: '累计发运金额'
            }),
            dataIndex: 'addupTotalAmountInvoice',
            render: text => formatAmount(text)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.addupTotalQuantityInvoice',
                defaultMessage: '累计发运数量'
            }),
            dataIndex: 'addupTotalQuantityInvoice'
        }, {
            title: formatMessage({
                id: 'tablePanel.column.addupVarietyRate',
                defaultMessage: '累计品种满足率'
            }),
            dataIndex: 'addupVarietyRate',
            render: text => formatPercent(text)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.addupMoneyRate',
                defaultMessage: '累计金额满足率'
            }),
            dataIndex: 'addupMoneyRate',
            render: text => formatPercent(text)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.addupQuantityRate',
                defaultMessage: '累计数量满足率'
            }),
            dataIndex: 'addupQuantityRate',
            render: text => formatPercent(text)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.shipmentConfirmTime',
                defaultMessage: '出运确认书日期'
            }),
            dataIndex: 'shipmentConfirmTime',
            render: text => formatDateTime(text, DATETIME_FORMAT)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.outboundDate',
                defaultMessage: '出库日期'
            }),
            dataIndex: 'outboundDate',
            render: text => formatDateTime(text, DATETIME_FORMAT)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.shipmentDate',
                defaultMessage: '出运日期'
            }),
            dataIndex: 'shipmentDate',
            render: text => formatDateTime(text, DATETIME_FORMAT)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.stockDay',
                defaultMessage: '备货天数'
            }),
            dataIndex: 'stockDay'
        }, {
            title: formatMessage({
                id: 'tablePanel.column.dealerConfirmDay',
                defaultMessage: '总代确认天数'
            }),
            dataIndex: 'dealerConfirmDay'
        }, {
            title: formatMessage({
                id: 'tablePanel.column.outboundDay',
                defaultMessage: '出库天数'
            }),
            dataIndex: 'outboundDay'
        }, {
            title: formatMessage({
                id: 'tablePanel.column.documentDay',
                defaultMessage: '单证操作天数'
            }),
            dataIndex: 'documentDay'
        }, {
            title: formatMessage({
                id: 'tablePanel.column.totalShipmentDay',
                defaultMessage: '总出运天数'
            }),
            dataIndex: 'totalShipmentDay'
        }, {
            title: formatMessage({
                id: 'tablePanel.column.actualArrivalDate',
                defaultMessage: '实际到港日期'
            }),
            dataIndex: 'actualArrivalDate',
            render: text => formatDateTime(text, DATETIME_FORMAT)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.goLibraryDate',
                defaultMessage: '到库日期'
            }),
            dataIndex: 'goLibraryDate',
            render: text => formatDateTime(text, DATETIME_FORMAT)
        }];
        const pagination = {
            total,
            pageSize,
            current: pageIndex + 1,
            onShowSizeChange: this.props.onPageSizeChange,
            onChange: this.props.onPageIndexChange,
            ...PAGINATION_OPTIONS
        };
        const locale = {
            emptyText: this.props.hasError ? COMMON_TABLE_QUERY_FAIL_TEXT : COMMON_TABLE_EMPTY_TEXT
        };
        return (
            <Card>
                <Table
                    className="white-space-nowrap"
                    columns={columns}
                    dataSource={this.props.data}
                    rowKey="id"
                    pagination={pagination}
                    loading={this.props.loading || this.state.loading}
                    onChange={this.onChangeSort}
                    locale={locale}
                    {...TABLE}/>
            </Card>
        );
    }
}

TablePanel.propTypes = {
    data: PropTypes.array.isRequired,
    hasError: PropTypes.string.isRequired,
    loading: PropTypes.bool.isRequired,
    total: PropTypes.number.isRequired,
    onChangeSort: PropTypes.func.isRequired,
    onPageIndexChange: PropTypes.func.isRequired,
    onPageSizeChange: PropTypes.func.isRequired,
    intl: PropTypes.object,
    pageIndex: PropTypes.number,
    pageSize: PropTypes.number,
};
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
import {tableSearch} from './actions';
const getData = selectorFactory(['page', 'domainData', 'list', 'data']);
const mapStateToProps = state => ({
    data: getData(state),
    loading: state.getIn(['page', 'domainData', 'list', 'isFetching']),
    hasError: state.getIn(['page', 'domainData', 'list', 'hasError']),
    total: state.getIn(['page', 'domainData', 'list', 'total']),
    pageIndex: state.getIn(['page', 'appState', 'queryBySort', 'pageIndex']),
    pageSize: state.getIn(['page', 'appState', 'queryBySort', 'pageSize']),
});

const mapDispatchToProps = dispatch => ({
    onPageSizeChange: (current, pageSize) =>
        dispatch(
            tableSearch({
                pageIndex: PAGE.index,
                pageSize
            })
        ),
    onPageIndexChange: pageIndex =>
        dispatch(
            tableSearch({
                pageIndex: pageIndex - 1
            })
        ),
    onChangeSort: option => dispatch(tableSearch(option))
});
import {connect} from 'react-redux';
import {injectIntl} from './intl';
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(TablePanel));
